/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddCustomer.java
 *
 * Created on Nov 5, 2011, 4:19:06 PM
 */
package mpinventory;

import java.sql.*;
import java.util.List;
import java.util.ArrayList;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
/**
 *
 * @author msi
 */
public class AddCustomer extends javax.swing.JPanel {

    private Connection conn;
    private Dimension size;
    private CustomerQuery customerQuery;
    public int width;
    public int height;
    public int screenWidth;
    public int screenHeight;
    public boolean backToMainMenu=false;
    /** Creates new form AddCustomer */
    public AddCustomer() {
        initComponents();
    }
    public AddCustomer(Connection conn, int screenWidth, int screenHeight) {
        this.conn=conn;
        this.screenWidth=screenWidth;
        this.screenHeight=screenHeight;
        initComponents();
        size=this.getPreferredSize();
        width=size.width;
        height=size.height;
        customerQuery=new CustomerQuery(conn);
        txtCACustID.setText(customerQuery.setID()+"");
    }
    @Override
    public int getWidth(){
        return width;
    }
    @Override
    public int getHeight(){
        return height;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtCAName = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtCAAddress = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        btnCASave = new javax.swing.JButton();
        txtCACustID = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        btnCACancel = new javax.swing.JButton();
        txtCAConNum = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtCARemarks = new javax.swing.JTextField();

        txtCAName.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        jLabel5.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        jLabel5.setText("Address:");

        txtCAAddress.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N
        txtCAAddress.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        jLabel4.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel4.setText("Name:");

        btnCASave.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        btnCASave.setText("Save");
        btnCASave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        txtCACustID.setEditable(false);
        txtCACustID.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        jLabel2.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel2.setText("Customer ID:");

        jLabel1.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 25)); // NOI18N
        jLabel1.setText("Add Customer Information");

        btnCACancel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        btnCACancel.setText("Cancel");
        btnCACancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        txtCAConNum.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N

        jLabel6.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        jLabel6.setText("<html>Contact<br> Number/s:</html>");

        jLabel7.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        jLabel7.setText("Remarks:");

        txtCARemarks.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(txtCACustID, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(74, 74, 74)
                                .addComponent(txtCAName, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(67, 67, 67)
                                .addComponent(btnCASave, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(64, 64, 64)
                                .addComponent(btnCACancel, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(42, 42, 42)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtCARemarks, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtCAConNum, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtCAAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap(38, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel2))
                    .addComponent(txtCACustID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel4))
                    .addComponent(txtCAName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(txtCAAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCAConNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel7))
                    .addComponent(txtCARemarks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCASave)
                    .addComponent(btnCACancel))
                .addContainerGap(36, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private boolean checkNumber(String number){
        if(number.length()==7||number.length()==11){
            for(int i=0;i<number.length();i++)
                if(number.charAt(i)=='1' || number.charAt(i)=='2' || number.charAt(i)=='3' || number.charAt(i)=='4' || number.charAt(i)=='5' || number.charAt(i)=='6' || number.charAt(i)=='7' || number.charAt(i)=='8' || number.charAt(i)=='9' || number.charAt(i)=='0')
                    ;
                else
                    return false;
        }
        else
            return false;
        return true;
    }
    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        if(txtCAName.getText().equals("") && txtCAAddress.getText().equals("") && txtCAConNum.getText().equals("") && txtCARemarks.getText().equals(""))
            backToMainMenu=true;
        else{
            JOptionPane pane=new JOptionPane("Are you sure you want to go back to the main menu? Please make sure that all changes have been saved.");
            Object[] options=new String[]{"Yes","No"};
            pane.setOptions(options);
            JDialog dialog = pane.createDialog(new JFrame(), "Back To Main Menu?");
            dialog.show();
            if(options[0].equals(pane.getValue())) //YES
                backToMainMenu=true;
        }
    }//GEN-LAST:event_cancelActionPerformed

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
        customerQuery=new CustomerQuery(conn);
        if(txtCAName.getText().equals("") || txtCAAddress.getText().equals("") || txtCAConNum.getText().equals("") || txtCARemarks.getText().equals(""))
            JOptionPane.showMessageDialog(null, "All fields cannot be empty!");
        else if(!checkNumber(txtCAConNum.getText()))
            JOptionPane.showMessageDialog(null, "Invalid phone number!");
        else if(customerQuery.canAddCustomer(txtCAName.getText(), txtCAAddress.getText())){
            JOptionPane pane=new JOptionPane("Are you sure you want to add this customer?");
            Object[] options=new String[]{"Yes","No"};
            pane.setOptions(options);
            JDialog dialog = pane.createDialog(new JFrame(), "Confirm Customer Addition");
            dialog.show();
            if(options[0].equals(pane.getValue())){
                customerQuery.addCustomer(customerQuery.setID(),txtCAName.getText(),txtCAAddress.getText(),txtCAConNum.getText(),txtCARemarks.getText());
                backToMainMenu=true;
            } //YES
        }
        else
            JOptionPane.showMessageDialog(null,txtCAName.getText()  + " already exists in the database!");
    }//GEN-LAST:event_saveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCACancel;
    private javax.swing.JButton btnCASave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField txtCAAddress;
    private javax.swing.JTextField txtCAConNum;
    private javax.swing.JTextField txtCACustID;
    private javax.swing.JTextField txtCAName;
    private javax.swing.JTextField txtCARemarks;
    // End of variables declaration//GEN-END:variables
}
